package cn.szv5.register.server;

/**
 * 应用模块名称<p> 代码描述<p> Copyright: Copyright (C) 2022 , Inc. All rights reserved. <p> Company: 阿飞工作室<p>
 *
 * @author Carl
 * @since 2022/2/8 10:27
 */
public class RegisterServerController {

    private ServiceRegistry register = ServiceRegistry.getInstance();

    /**
     * 发送注册请求
     *
     * @param request
     * @return
     */
    public RegisterResponse register(RegisterRequest request) {
        RegisterResponse registerResponse = new RegisterResponse();

        try {
            // 在注册表中加入这个服务实例
            ServiceInstance serviceInstance = new ServiceInstance();
            serviceInstance.setHostname(request.getHostname());
            serviceInstance.setIp(request.getIp());
            serviceInstance.setPort(request.getPort());
            serviceInstance.setServiceName(request.getServiceName());
            serviceInstance.setServiceInstanceId(request.getServiceInstanceId());

            register.register(serviceInstance);

            // 更新自我保护机制的阈值
            synchronized (SelfProtectionPolicy.class) {
                SelfProtectionPolicy selfProtectionPolicy = SelfProtectionPolicy.getInstance();
                selfProtectionPolicy.setExpectedHeartbeatRate(selfProtectionPolicy.getExpectedHeartbeatRate() + 2);
                selfProtectionPolicy.setExpectedHeartbeatThreshold(
                    (long)(selfProtectionPolicy.getExpectedHeartbeatRate() * 0.85));
            }

            registerResponse.setStatus(RegisterResponse.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            registerResponse.setStatus(RegisterResponse.FAILURE);
        }
        return registerResponse;
    }

    /**
     * 发送心跳请求
     *
     * @param request
     * @return
     */
    public HeartbeatResponse heartbeat(HeartbeatRequest request) {
        HeartbeatResponse heartbeatResponse = new HeartbeatResponse();

        try {
            // 对服务进行续约
            ServiceInstance serviceInstance =
                register.getServiceInstance(request.getServiceName(), request.getServiceInstanceId());
            serviceInstance.renew();

            // 记录一下每分钟心跳的次数
            HeartbeatCounter heartbeatMessuredRate = HeartbeatCounter.getInstance();
            heartbeatMessuredRate.increment();

            heartbeatResponse.setStatus(HeartbeatResponse.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            heartbeatResponse.setStatus(HeartbeatResponse.FAILURE);
        }

        return heartbeatResponse;
    }

    /**
     * 拉取全量注册表
     *
     * @return
     */
    public Applications fetchFullRegistry() {
        try {
            register.readLock();
            return new Applications(register.getRegistry());
        } finally {
            register.readUnlock();
        }
    }

    /**
     * 拉取增量注册表
     *
     * @return
     */
    public DeltaRegistry fetchDeltaRegistry() {
        try {
            register.readLock();
            return register.getDeltaRegistry();
        } finally {
            register.readUnlock();
        }
    }

    /**
     * 服务下线
     */
    public void cancel(String serviceName, ServiceInstance serviceInstance) {
        register.remove(serviceName, serviceInstance);

        // 更新自我保护机制的阈值
        synchronized (SelfProtectionPolicy.class) {
            SelfProtectionPolicy selfProtectionPolicy = SelfProtectionPolicy.getInstance();
            selfProtectionPolicy.setExpectedHeartbeatRate(selfProtectionPolicy.getExpectedHeartbeatRate() - 2);
            selfProtectionPolicy.setExpectedHeartbeatThreshold(
                (long)(selfProtectionPolicy.getExpectedHeartbeatRate() * 0.85));
        }
    }

}
